import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, ruleType, status, page = 1, pageSize = 10 } = query;

  // 模拟调度规则数据
  const rules = [
    {
      id: '1',
      name: 'VIP客户优先规则',
      code: 'VIP_PRIORITY_RULE',
      ruleType: 'PRIORITY',
      description: 'VIP客户预约时享有最高优先级',
      priority: 1,
      conditions: {
        customerType: 'VIP',
        resourceType: 'ALL',
      },
      actions: {
        priority: 10,
        autoConfirm: true,
      },
      status: 'ACTIVE',
      createTime: '2024-01-15 10:30:00',
      updateTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      name: '工作时间规则',
      code: 'WORK_TIME_RULE',
      ruleType: 'TIME_SLOT',
      description: '限制预约时间在工作时间内',
      priority: 2,
      conditions: {
        startTime: '09:00',
        endTime: '18:00',
        daysOfWeek: [1, 2, 3, 4, 5],
      },
      actions: {
        rejectOutsideHours: true,
        message: '预约时间必须在工作时间内',
      },
      status: 'ACTIVE',
      createTime: '2024-01-15 09:15:00',
      updateTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      name: '资源容量规则',
      code: 'CAPACITY_RULE',
      ruleType: 'CAPACITY',
      description: '检查资源容量是否足够',
      priority: 3,
      conditions: {
        resourceType: 'PHYSICAL',
        minCapacity: 1,
      },
      actions: {
        checkCapacity: true,
        rejectIfFull: true,
      },
      status: 'ACTIVE',
      createTime: '2024-01-15 08:45:00',
      updateTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      name: '冲突检测规则',
      code: 'CONFLICT_RULE',
      ruleType: 'CONFLICT',
      description: '检测预约时间冲突',
      priority: 4,
      conditions: {
        checkOverlap: true,
        bufferTime: 30,
      },
      actions: {
        rejectConflict: true,
        suggestAlternative: true,
      },
      status: 'ACTIVE',
      createTime: '2024-01-14 16:20:00',
      updateTime: '2024-01-14 16:20:00',
    },
    {
      id: '5',
      name: '节假日规则',
      code: 'HOLIDAY_RULE',
      ruleType: 'TIME_SLOT',
      description: '节假日特殊预约规则',
      priority: 5,
      conditions: {
        isHoliday: true,
        specialHours: true,
      },
      actions: {
        extendedHours: true,
        specialPricing: true,
      },
      status: 'INACTIVE',
      createTime: '2024-01-14 15:10:00',
      updateTime: '2024-01-14 15:10:00',
    },
  ];

  // 过滤数据
  let filteredData = rules;

  if (keyword) {
    filteredData = filteredData.filter(
      (item) =>
        item.name.includes(keyword as string) ||
        item.code.includes(keyword as string),
    );
  }

  if (ruleType) {
    filteredData = filteredData.filter(
      (item) => item.ruleType === ruleType,
    );
  }

  if (status) {
    filteredData = filteredData.filter(
      (item) => item.status === status,
    );
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
